<?php
/*
Template Name: Events Page
*/
?>

<?php get_header(); ?>

<?php

	$querystr = "
		SELECT wposts.*
		FROM $wpdb->posts wposts
			LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id 
			LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id)
			LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)	
		WHERE wposts.ID = wpostmeta.post_id 
		AND wpostmeta.meta_key = '_publish_meta_begin_date'
		AND wpostmeta.meta_value != ''
		AND wposts.post_type = 'news'
			AND wposts.post_status = 'publish'
			AND $wpdb->term_taxonomy.taxonomy = 'category'
			AND $wpdb->term_taxonomy.term_id IN(3)
		ORDER BY wpostmeta.meta_value ASC
	"; 


$pageposts = $wpdb->get_results($querystr, OBJECT);

?>
<div id="content">
<?php
if ($pageposts): 
	global $post; 
	foreach ($pageposts as $post): 
	setup_postdata($post); 

//print_r($news);

?>

	<?php //if ( $news->have_posts() ) while ( $news->have_posts() ) : $news->the_post(); ?>
		<div id="post-<?php the_ID(); ?>" <?php post_class('page'); ?>>
			<article>
				<h1><?php the_title(); ?></h1>
                <?php 
                	$meta['begin_date'] = get_post_meta($post->ID,'_publish_meta_begin_date',TRUE);
 					$meta['end_date'] = get_post_meta($post->ID,'_publish_meta_end_date',TRUE);
					$meta['publish_end'] = get_post_meta($post->ID,'_publish_meta_publish_end',TRUE);
				echo $meta['begin_date'] . " - " . $meta['end_date'];
				?>
                
				<?php echo '<div class="featured-thumbnail">'; the_post_thumbnail(); echo '</div>'; /* loades the post's featured thumbnail, requires Wordpress 3.0+ */ ?>
				<div class="post-content page-content">
					<?php the_content(); ?>
					<?php wp_link_pages('before=<div class="pagination">&after=</div>'); ?>
				</div><!--.post-content .page-content -->
			</article>


		</div><!--#post-# .post-->
 <?php endforeach; ?>

 <?php endif; ?>

	<?php // endwhile; ?>
</div><!--#content-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
